package link.luyu.plugin.brochain;

import link.luyu.plugin.brochain.types.BlockAndPage;
import link.luyu.plugin.brochain.web3.methods.response.Page;
import link.luyu.plugin.brochain.web3.methods.response.TransactionReceipt;
import link.luyu.protocol.network.Block;
import link.luyu.protocol.network.Receipt;

public class Converter {

    private final String chainPath;

    public Converter(String chainPath) {
        this.chainPath = chainPath;
    }

    public Block toBlock(BlockAndPage blockAndPage) {
        Block luyuBlock = new Block();
        luyuBlock.setChainPath(chainPath);
        luyuBlock.setNumber(blockAndPage.getBlock().getNumber().longValue());
        luyuBlock.setHash(blockAndPage.getBlock().getHash());
        luyuBlock.setParentHash(new String[] {blockAndPage.getBlock().getParentHash()});
        luyuBlock.setRoots(
                new String[] {
                    blockAndPage.getBlock().getTransactionsRoot(),
                    blockAndPage.getBlock().getStateRoot(),
                    blockAndPage.getBlock().getReceiptsRoot(),
                    blockAndPage.getBlock().getPagesRoot(),
                });
        luyuBlock.setTimestamp(blockAndPage.getBlock().getTimestamp().longValue());
        luyuBlock.setBytes(blockAndPage.serialize());
        return luyuBlock;
    }

    public Receipt toReceipt(TransactionReceipt transactionReceipt) {
        Receipt receipt = new Receipt();
        receipt.setTransactionHash(transactionReceipt.getTransactionHash());
        return receipt;
    }

    public Receipt toReceipt(Page.PageTransaction pageTransaction) {
        Receipt receipt = new Receipt();
        receipt.setTransactionHash(pageTransaction.getHash());
        return receipt;
    }
}
